import plugin from '../../../lib/plugins/plugin.js';

export class sign extends plugin {
  constructor() {
    super({
      name: '[Xs插件]公共签名状态检测',
      dsc: '公共签名状态检测',
      event: 'message',
      priority: 9,
      rule: [{
        reg: `^#?公共签名状态$`,
        fnc: `publicSign`
      }]
    })
  }

  async publicSign(e) {
    if (/#?公共签名状态/.test(e.msg)) {
      let url = ''; // 定义一个url变量
      // 定义一个对象，存储每个链接的名字和版本
      let publicUrls = {
        'https://QSignX-X5.hf.space?key=X': {
          name: 'QSignX-X5',
          version: '8.9.78'
        },
        'https://QSignX-X4.hf.space?key=X': {
          name: 'QSignX-X4',
          version: '8.9.78'
        },
        'https://QSignX-X3.hf.space?key=X': {
          name: 'QSignX-X3',
          version: '8.9.78'
        },
        'https://QSignX-X2.hf.space?key=X': {
          name: 'QSignX-X2',
          version: '8.9.78'
        },
        'https://QSignX-X1.hf.space?key=X': {
          name: 'QSignX-X1',
          version: '8.9.78'
        },
        'http://cn-hk-bgp-4.openfrp.top:33486/sign?key=114514': {
                      name: 'xs-plugin2',
                                version: '8.9.88'
        },
        
      }
      let reply = '';
      e.reply('正在检测接口连通性...', true);
      // 遍历对象的键，即链接
      for (let publicUrl of Object.keys(publicUrls)) {
        url = publicUrl; // 把公共签名链接赋值给url变量
        // 从对象中获取对应链接的名字和版本
        let name = publicUrls[publicUrl].name;
        let version = publicUrls[publicUrl].version;
        try {
          let res = await fetch(url);
          res = await res.json();
          // 判断json文件是否符合正常的格式
          if ((res.code == 1 && res.msg == "Parameter 'uin' is missing." && res.data == "failed") || (res.code == 0 && res.msg == "IAA 云天明 章北海" && res.data.version == "1.1.9" && res.data.protocol.qua == "V1_AND_SQ_8.9.78_4548_YYB_D" && res.data.protocol.version == "8.9.78" && res.data.protocol.code == "4548" && res.data.protocol.package_name == "com.tencent.mobileqq")) {
            reply += `签名名字：${name}\n签名版本：${version}\n状态：正常✅\n`;
          } else {
            reply += `签名名字：${name}\n签名版本：${version}\n状态：异常❗\n`;
          }
        } catch (err) {
          reply += `签名名字：${name}\n签名版本：${version}\n状态：异常❗\n`;
        }
      }
      e.reply(reply, true);
      return true;
    }
  }
}